package com.kh.pajx.sop.service.impl;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kh.pajx.sop.base.BaseServiceImpl;
import com.kh.pajx.sop.dao.IHsEquipmentInfoDao;
import com.kh.pajx.sop.domain.HsEquipmentInfo;
import com.kh.pajx.sop.domain.PageBean;
import com.kh.pajx.sop.service.IHsEquipmentInfoService;
import com.kh.pajx.sop.util.page.QueryHelper;

@Service
@Transactional
public class HsEquipmentInfoServiceImpl extends BaseServiceImpl<HsEquipmentInfo> implements IHsEquipmentInfoService {
	@Resource
	private IHsEquipmentInfoDao hsEquipmentInfoDao;

	public HsEquipmentInfoServiceImpl() {
	}

	@Override

	public void add(HsEquipmentInfo hsEquipmentInfo) throws Exception{
		
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
		String createTime=sdf.format(new Date());
		//设置设备的创建时间为当前时间,关联关系状态默认为1
		hsEquipmentInfo.setCreateTime(createTime);
		//hsEquipmentInfo.setRelationStatusFlag("1");
		//当硬件类型为空时，默认为0
		if(("").equals(hsEquipmentInfo.getEquNameDc())){
			hsEquipmentInfo.setEquNameDc("0");
		}
		//当设备类型为"1"，表示"亲情电话" 
		//把equNameDc硬件名称,equAttType考勤类型 ,equAttInoutType进出类型  赋值保存
		if("1".equals(hsEquipmentInfo.getEquType())){
			hsEquipmentInfo.setEquNameDc("0");
			//hsEquipmentInfo.setEquAttType("4");
			hsEquipmentInfo.setEquAttInoutType("0");
		}
		hsEquipmentInfoDao.save(hsEquipmentInfo);
		
	}
	@Override
	public void update(HsEquipmentInfo hsEquipmentInfo) {

		/*SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
		String createTime=sdf.format(new Date());
		//设置设备的最后通讯时间为当前时间 
		hsEquipmentInfo.setEquLastTime(createTime);*/
		//HsEquipmentInfo eq=getByEquNo(hsEquipmentInfo.getEquNo());
	        HsEquipmentInfo eq=hsEquipmentInfoDao.getEquByEquNo(hsEquipmentInfo.getEquNo());
		if(("").equals(hsEquipmentInfo.getEquNameDc())){
			hsEquipmentInfo.setEquNameDc("0");
		}
		//当设备类型发生变化
		if("0".equals(hsEquipmentInfo.getEquType())){
			eq.setEquAttType(hsEquipmentInfo.getEquAttType());
			eq.setEquAttInoutType(hsEquipmentInfo.getEquAttInoutType());
			eq.setEquNameDc(hsEquipmentInfo.getEquNameDc());
		}
		if("1".equals(hsEquipmentInfo.getEquType())){
			//eq.setEquAttType("4");
			eq.setEquAttInoutType("0");
			eq.setEquNameDc("");
		}
		
		eq.setEquStausFlag(hsEquipmentInfo.getEquStausFlag());
		eq.setEquType(hsEquipmentInfo.getEquType());
		eq.setEquSip(hsEquipmentInfo.getEquSip());
		eq.setEquSport(hsEquipmentInfo.getEquSport());
		eq.setEquApnSim(hsEquipmentInfo.getEquApnSim());
		eq.setEquHardVer(hsEquipmentInfo.getEquHardVer());
		eq.setEquSoftVer(hsEquipmentInfo.getEquSoftVer());
		eq.setEquDescrip(hsEquipmentInfo.getEquDescrip());
		eq.setRemark(hsEquipmentInfo.getRemark());
		hsEquipmentInfoDao.update(eq);
	}
	@Override

	public PageBean getPageBean(int pageNum, int pageSize,
			QueryHelper queryHelper) {
		return hsEquipmentInfoDao.getPageBean(pageNum, pageSize, queryHelper);
	}

	/*根据设备编号去删除设备，包括批量删除*/
	@Override
	public int deleteByequNo(String equNos) {
		int returnNum=0;
		if(StringUtils.isNotEmpty(equNos)){
			String[] equimpNos = equNos.split(",");
			Long[] equipmentNos = new Long[equimpNos.length];
			returnNum = equipmentNos.length;
			//List<Long> equipmentNos = new ArrayList<Long>();
			for (int i = 0;i< equipmentNos.length; i++) {
				equipmentNos[i] = Long.valueOf(equimpNos[i]);
			}
			hsEquipmentInfoDao.updateByEquNo(equipmentNos);
		}
		return returnNum;
	}
	
	/*根据设备编号查询 */
	@Override
	public List<HsEquipmentInfo> get(Long equNo) {
		return hsEquipmentInfoDao.findList(" FROM HsEquipmentInfo d WHERE d.equNo="+equNo+" AND d.equStausFlag='1'");
	}
	
	/*通过hql查询设备信息*/
	@Override
	public List<HsEquipmentInfo> findByHql(String sclId,String equType) {
		return hsEquipmentInfoDao.findList(" FROM HsEquipmentInfo d WHERE d.hsSchoolInfo.sclId='"
				+ sclId + "' AND d.equType='" + equType + "' AND d.equStausFlag = '1'");
	}
	
	
	@Override
	public List<HsEquipmentInfo> getByIds(String[] uuids) {
		return hsEquipmentInfoDao.getByIds(uuids);
	}

	@Override
	public List<HsEquipmentInfo> getByNos(String[] equNos) {
		Long[] nos = new Long[equNos.length];
		 for(int i = 0;i<equNos.length;i++){
			 nos[i]=Long.valueOf(equNos[i]);
		 }
		 return hsEquipmentInfoDao.getAllEquByEquNo(nos);
	}

	@Override
	public HsEquipmentInfo getByEquNo(Long equNo) {
		Long[] nos = new Long[]{equNo};
		return hsEquipmentInfoDao.getAllEquByEquNo(nos).get(0);
	}


}
